<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of HTC
 *
 * @author dragon.hu add @2016.2.24
 */
require_once 'BaseChannel.php';

//HTC
class Htc implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbRLzWfCD4pQb1mjeGLy6gw+AfOKZ1dpNbMUyZml+p3stTSdTyHHpkuPPsaOqsT9gFDSmXz5KRBt4w6KCeLj/R61KA5rmMJipDnSJV19kld0z6NW47kiEQHslaalDBCST94TUIcCzjhaiG3yTChDCTFo3v47qyt6j3YvVpih8UNQIDAQAB"; //public key
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("htc", $_REQUEST);
		
		$input = file_get_contents("php://input");//$GLOBALS['HTTP_RAW_POST_DATA'];
		$orderLog->log("htc", $input);
		parse_str($input, $data);
		
		if(!isset($data['order']) || !isset($data['sign'])) {
			$orderLog->log("htc", "POST data empty");
			return "fail0 data error";
		}
		
		$order = substr(urldecode($data['order']), 1, -1);
		$sign = $data['sign'];
		
		$pub_key=self::setupPubKey($paymentKey); //$AppSecret 为pubkey
		
		$dataStatus=self::rsa_verify($order,$sign,$pub_key);
		
		if($dataStatus) 
		{
			$orderData=json_decode($order,true);
			$result_code = $orderData['result_code']; //1代表支付成功 0代表支付失败
			$gmt_create = $orderData['gmt_create']; //支付订单创建时间 格式：yyyy-MM-dd HH:mm:ss;
			$amount = $orderData['real_amount']; //用户真实付费金额，单位为分
			$result_msg = $orderData['result_msg']; //支付结果提示语，成功=支付成功；失败=支付失败
			$game_code = $orderData['game_code']; //游戏编号
			$game_order_id = $orderData['game_order_id']; //游戏订单id ,透传
			$orderId = $orderData['jolo_order_id']; //jolo支付订单id
			$gmt_payment = $orderData['gmt_payment']; //支付时间
			
			$tmpstatus = STATUS_SUCCESS;
			if ($result_code != 1)
			{
				$tmpstatus = STATUS_FAILD;
				//return "fail3 data pay invalid";
			}
			
			if(false == $orderLog->recordOrder($tmpstatus, $amount/100, $game_order_id, "htc", $platform,$orderId))
			{
				$orderLog->log("htc",array("order"=>$game_order_id,"money"=>$amount,"serverresult"=>"faild"));
			}
			else
			{
				//支付成功
				$orderLog->log("htc",array("order"=>$game_order_id,"money"=>$amount,"serverresult"=>"success"));
			}
		}
		else
		{
			$orderLog->log("htc","rsa verify error");
			//return "fail2 data invalid";
		}
		
		return "success";
	}
	
	static function setupPubKey($pubKey) {
		if (is_resource ( $pubKey )) {
			return true;
		}
			
		$pem = chunk_split($pubKey,64,"\n");
		$pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
		$pub_Key= openssl_pkey_get_public ( $pem );
		return $pub_Key;
	}
	static function  rsa_verify($data, $signature, $public_key) {
		if(empty($data) || empty($signature)) {
			return false;
		}
		$pub_res = openssl_get_publickey($public_key);
		return openssl_verify($data, base64_decode($signature), $public_key );
	}
	
}